package io.github.wslxm.springbootplus2.manage.login.controller;

import io.github.wslxm.springbootplus2.core.base.model.BaseIds;
import io.github.wslxm.springbootplus2.core.base.model.BasePage;
import io.github.wslxm.springbootplus2.core.constant.BaseConstant;
import io.github.wslxm.springbootplus2.core.result.Result;
import io.github.wslxm.springbootplus2.core.utils.XjBeanUtil;
import io.github.wslxm.springbootplus2.manage.sys.model.dto.SysUserDTO;
import io.github.wslxm.springbootplus2.manage.sys.model.query.SysMsgQuery;
import io.github.wslxm.springbootplus2.manage.sys.model.query.next.SysMenuTreeQuery;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysMenuVO;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysMsgVO;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.SysUserVO;
import io.github.wslxm.springbootplus2.manage.sys.model.vo.next.SysMsgFindAllNumVO;
import io.github.wslxm.springbootplus2.manage.sys.service.SysMenuService;
import io.github.wslxm.springbootplus2.manage.sys.service.SysMsgService;
import io.github.wslxm.springbootplus2.manage.sys.service.SysUserService;
import io.github.wslxm.springbootplus2.utils.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 登录用户相关数据 前端控制器
 *
 * @author wangsong
 * @date 2023/05/17
 */
@RestController
@RequestMapping(BaseConstant.Uri.API_ADMIN + "/sys/loginUser")
@Tag(name = "base--sys--登录用户相关")
public class LoginUserController {

    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysMsgService sysMsgService;
    @Autowired
    private HttpServletRequest request;

    @GetMapping(value = "/menu/tree")
    @Operation(summary = "查询当前登录人的菜单", description = "根据sort正序排序返回, isLoginUser 默认等于 true")
    public Result<List<SysMenuVO>> tree(@ModelAttribute SysMenuTreeQuery query) {
        query.setIsLoginUser(true);
        return Result.success(sysMenuService.tree(query));
    }

    @PutMapping(value = "/updUser")
    @Operation(summary = "修改当前登录人的信息")
    public Result<Boolean> updUser(@RequestBody SysUserDTO dto) {
        return Result.successUpdate(sysUserService.upd(JwtUtil.getJwtUser(request).getUserId(), dto));
    }

    @PutMapping(value = "/updByPassword")
    @Operation(summary = "修改当前登录人的密码", description = "判断原密码是否正确,不正确返回错误信息msg ,正确直接修改,密码进行MD5加密 --> val(前端输入密码值)+盐(后端规则指定)=最终密码）")
    public Result<Boolean> updByPassword(@RequestParam String oldPassword, @RequestParam String password) {
        return Result.success(sysUserService.updByPassword(oldPassword, password));
    }

    @GetMapping(value = "/findUser")
    @Operation(summary = "查询当前登录人的个人信息")
    public Result<SysUserVO> findUser() {
        return Result.successFind(XjBeanUtil.convert(sysUserService.findId(JwtUtil.getJwtUser(request).getUserId()), SysUserVO.class));
    }

    @PutMapping(value = "/msg/{id}/read")
    @Operation(summary = "消息修改为已读-指定id (仅支持修改当前登录用户)")
    public Result<Boolean> updRead(@PathVariable String id) {
        return Result.successUpdate(sysMsgService.updRead(id));
    }

    @PutMapping(value = "/msg/readByIds")
    @Operation(summary = "消息修改为已读-指定ids (仅支持修改当前登录用户)")
    public Result<Boolean> updReadByIds(@RequestBody BaseIds dto) {
        return Result.success(sysMsgService.updReadByIds(dto.getIds()));
    }

    @PutMapping(value = "/msg/readAll")
    @Operation(summary = "消息修改为已读-全部 (当前登录用户)")
    public Result<Boolean> readAll() {
        return Result.success(sysMsgService.readAll());
    }

    @Operation(summary = "查询未读数量 (当前登录用户)")
    @GetMapping(value = "/msg/findUnreadNum")
    public Result<Long> findUnreadNum() {
        return Result.successFind(sysMsgService.findUnreadNum());
    }

    @Operation(summary = "查询所有消息数量-包括全部/已读/未读 (当前登录用户)")
    @GetMapping(value = "/msg/findAllNum")
    public Result<SysMsgFindAllNumVO> findAllNum() {
        return Result.successFind(sysMsgService.findAllNum());
    }

    @GetMapping(value = "/msg/findPage")
    @Operation(summary = "查询通知消息 (当前登录用户)")
    public Result<BasePage<SysMsgVO>> findPage(@ModelAttribute @Validated SysMsgQuery query) {
        query.setIsLoginUser(true);
        return Result.success(sysMsgService.findPage(query));
    }
}
